﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace GPSTrackingWebsiteManager.Manager.Pages
{
    public partial class GPSCommands : ClassManager.SessionManagerPage
    {
        private void GetAllGPSModels()
        {
            GPSModelsGrid.DataSource = GPSTrackingDBV2.Data.DataRepository.GpsModelsProvider.GetAll();
        }

        private void GetAllGpsCommand()
        {
            if (ViewState["GpsModelId"] != null)
            {
                int GpsModelID = Convert.ToInt32(ViewState["GpsModelId"]);
                GPSCommandGrid.DataSource = GPSTrackingDBV2.Data.DataRepository.GpsCommandsProvider.GetByGpsModelId(GpsModelID);                
            }
        }

        private GPSTrackingDBV2.Entities.GpsCommands Invoke_Entity(Hashtable Entity)
        {
            GPSTrackingDBV2.Entities.GpsCommands GpsCommandEntity = new GPSTrackingDBV2.Entities.GpsCommands();

            GpsCommandEntity.GpsCommandId = Convert.ToInt32(Entity[GPSTrackingDBV2.Entities.GpsCommandsColumn.GpsCommandId.ToString()]);
            GpsCommandEntity.GpsModelId = Convert.ToInt32(Entity[GPSTrackingDBV2.Entities.GpsCommandsColumn.GpsModelId.ToString()]);
            GpsCommandEntity.OrderCommand = Convert.ToInt32(Entity[GPSTrackingDBV2.Entities.GpsCommandsColumn.OrderCommand.ToString()]);
            GpsCommandEntity.CommandText = Convert.ToString(Entity[GPSTrackingDBV2.Entities.GpsCommandsColumn.CommandText.ToString()]);

            return GpsCommandEntity;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GPSCommandGrid.DataSource = new System.Data.DataTable();
                
                GridSortExpression expression = new GridSortExpression();
                expression.FieldName = GPSTrackingDBV2.Entities.GpsModelsColumn.Name.ToString();
                expression.SetSortOrder("Ascending");
                this.GPSModelsGrid.MasterTableView.SortExpressions.AddSortExpression(expression);

                expression = new GridSortExpression();
                expression.FieldName = "GpsModelId";
                expression.SetSortOrder("Ascending");
                this.GPSCommandGrid.MasterTableView.SortExpressions.AddSortExpression(expression);
                
            }
        }

        protected void GPSModelsGrid_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            GetAllGPSModels();
            ResizeGrid();
        }
        
        protected void GPSModelsGrid_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridDataItem item = (GridDataItem)GPSModelsGrid.SelectedItems[0];
            if (item != null)
            {
                ViewState["GpsModelId"] = item["GpsModelId"].Text;
                GPSCommandGrid.Enabled = true;
                GetAllGpsCommand();
                GPSCommandGrid.DataBind();

                ResizeGrid();
            }
        }

        protected void GPSCommandGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            GetAllGpsCommand();
            ResizeGrid();
        }

        private void ResizeGrid()
        {
            GPSCommandGrid.Height = Unit.Parse(HiddenFieldHeight.Value);            
            GPSModelsGrid.Height = Unit.Parse(HiddenFieldHeight.Value);
        }

        protected void GPSCommandGrid_InsertCommand(object sender, GridCommandEventArgs e)
        {
            try
            {
                GridEditableItem item = e.Item as GridEditableItem;
                Hashtable Entity = new Hashtable();
                item.ExtractValues(Entity);

                GPSTrackingDBV2.Entities.GpsCommands GpsCommandEntity = Invoke_Entity(Entity);

                GpsCommandEntity.GpsCommandId = GPSTrackingDBV2Custom.Data.DataRepository.GpsCommandsProvider.GetNextID();
                GpsCommandEntity.GpsModelId = Convert.ToInt32(ViewState["GpsModelID"]);

                GPSTrackingDBV2.Data.DataRepository.GpsCommandsProvider.Insert(GpsCommandEntity);

            }
            catch (Exception ex)
            {
                ClassManager.CustomExceptionManager.Generate_Error_Object_RadGrid("Error - " + ex.Message, ref GPSModelsGrid);
            }
        }

        protected void GPSCommandGrid_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            try
            {
                GridEditableItem item = e.Item as GridEditableItem;
                Hashtable Entity = new Hashtable();
                item.ExtractValues(Entity);

                GPSTrackingDBV2.Entities.GpsCommands GpsCommandEntity = Invoke_Entity(Entity);                
                GpsCommandEntity.OriginalGpsCommandId = GpsCommandEntity.GpsCommandId;

                GPSTrackingDBV2.Data.DataRepository.GpsCommandsProvider.Update(GpsCommandEntity);

            }
            catch (Exception ex)
            {
                ClassManager.CustomExceptionManager.Generate_Error_Object_RadGrid("Error - " + ex.Message, ref GPSModelsGrid);
            }
        }

        protected void GPSCommandGrid_DeleteCommand(object sender, GridCommandEventArgs e)
        {
            try
            {
                GridEditableItem item = e.Item as GridEditableItem;
                Hashtable Entity = new Hashtable();
                item.ExtractValues(Entity);

                GPSTrackingDBV2.Entities.GpsCommands GpsCommandEntity = Invoke_Entity(Entity);                

                GPSTrackingDBV2.Data.DataRepository.GpsCommandsProvider.Delete(GpsCommandEntity);

            }
            catch (Exception ex)
            {
                ClassManager.CustomExceptionManager.Generate_Error_Object_RadGrid("Error - " + ex.Message, ref GPSModelsGrid);
            }
        }

        protected void lbClearFilters_Click(object sender, EventArgs e)
        {
            GPSModelsGrid.MasterTableView.FilterExpression = string.Empty;

            foreach (GridColumn column in GPSModelsGrid.MasterTableView.RenderColumns)
            {
                if (column is GridBoundColumn)
                {
                    GridBoundColumn boundColumn = column as GridBoundColumn;
                    boundColumn.CurrentFilterValue = string.Empty;
                }

                if (column is GridDropDownColumn)
                {
                    GridDropDownColumn dropDownColumn = column as GridDropDownColumn;
                    dropDownColumn.CurrentFilterValue = string.Empty;
                }
            }

            clearGPSCommandGrid();
            GPSModelsGrid.MasterTableView.Rebind();
            ResizeGrid();
        }

        protected void lbClearFilters2_Click(object sender, EventArgs e)
        {
            GPSModelsGrid.MasterTableView.FilterExpression = string.Empty;

            foreach (GridColumn column in GPSModelsGrid.MasterTableView.RenderColumns)
            {
                if (column is GridBoundColumn)
                {
                    GridBoundColumn boundColumn = column as GridBoundColumn;
                    boundColumn.CurrentFilterValue = string.Empty;
                }

                if (column is GridDropDownColumn)
                {
                    GridDropDownColumn dropDownColumn = column as GridDropDownColumn;
                    dropDownColumn.CurrentFilterValue = string.Empty;
                }
            }


            GPSModelsGrid.MasterTableView.Rebind();
            ResizeGrid();
        }


        public void showErrorMessage(string msg)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "objWindowsMessageManager.show_miniError", "objWindowsMessageManager.show_miniError('" + msg + "','Dispositivo SMS Comandos ...');", true);
        }

        protected void lbShowError_Click(object sender, EventArgs e)
        {
            showErrorMessage(HiddenFieldError.Value);
        }

        protected void GPSModelsGrid_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == RadGrid.SortCommandName || e.CommandName == RadGrid.FilterCommandName)
            {
                clearGPSCommandGrid();
            }
        }

        private void clearGPSCommandGrid()
        {
            GPSCommandGrid.DataSource = new string[] { };
            GPSCommandGrid.DataBind();
            ResizeGrid();
        }
       
    }
}